package com.example.meitao.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.meitao.R;
import com.example.meitao.adapter.ChunTuAdapter;
import com.example.meitao.bean.ChunTu;
import com.example.meitao.config.NetConfig;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 星星 on 2016/9/2.
 */
public class ZheKouFragment extends Fragment {
    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRecyclerView;
    private List<ChunTu.Itemsbean> data;
    private RecyclerView.Adapter adapter;
    private int mPosition = 1;
    private LinearLayoutManager layout;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tu_item,null);
        initView(view);
        initData();
        setData();
        return view;
    }

    private void setData() {
        layout = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(layout);
        mRecyclerView.setAdapter(adapter);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPosition = 1;
                loadMore(1);
//                mPosition++;
            }
        });
        mRefreshLayout.setColorSchemeColors(Color.CYAN,Color.GREEN);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(newState == RecyclerView.SCROLL_STATE_IDLE) {
                    int lastPosition = layout.findLastVisibleItemPosition();
                    if(lastPosition == data.size()-1) {
                        loadMore(mPosition);
                        mPosition++;
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }

    private void loadMore(final int mPosition1){
        String format = String.format(NetConfig.TU_PATH, mPosition1);
        x.http().get(new RequestParams(format),  new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                ChunTu content = gson.fromJson(result, ChunTu.class);
                mPosition++;
                if(mPosition1 == 1) {
                    data.clear();
                }
                data.addAll(content.getItems());
                adapter.notifyDataSetChanged();
                mRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(getActivity(), "网速不好就别上网啊", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    private void initData() {
        loadMore(1);
        data = new ArrayList<>();
        adapter = new ChunTuAdapter(data,getActivity());
    }


    private void initView(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_tu_test);
        mRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.wrl_tu_test);
    }
}

